import { _decorator, Component, EventTouch, Node, NodeEventType, Sprite, SpriteFrame } from 'cc';
import EntityBase from '../utils/EntitySystem/EntityBase';
import EventProp from '../utils/EventSystem/EventProp';
import EntityManager from '../utils/EntitySystem/EntityManager';
import DataManager from '../utils/DataSystem/DataManager';
import ResourceManager from '../utils/ResourceSystem/ResourceManager';
import { ProbeEventList } from '../utils/EventSystem/EventList';
const { ccclass, property } = _decorator;

export enum StarType
{
    red,
    blue,
    green,
    yellow,
    purple
}

@ccclass('Star')
export class StarEntity extends EntityBase {

    public static async Create() : Promise<StarEntity>
    {
        return await EntityManager.Instance.GetEntity("star");
    }

    public StarTouched : ProbeEventList = new ProbeEventList();

    public IsSelected : boolean = false;

    public Type : StarType;

    public col : number;
    public row : number;

    public InitItem(param: any): void {
        super.InitItem(param);

        let type = param[0] as StarType;
        let parent = param[1] as Node;
        
        this.Type = type;
        parent.addChild(this.node);

        this.node.on(NodeEventType.TOUCH_START,this._onTouchStart,this);
    }

    public CloseSelf(): void {
        this.col = 0;
        this.row = 0;
        this.StarTouched.Clear();
        super.CloseSelf();
    }

    public async UpdateStar()
    {
        let spriteKey = "bl4";
        switch (this.Type) {
            case StarType.red:
                spriteKey = "bl0";
                break;
            case StarType.blue:
                spriteKey = "bl1";
                break;    
            case StarType.green:
                spriteKey = "bl2";
                break;
            case StarType.yellow:
                spriteKey = "bl3";
                break;      
            default:
                break;
        }
        let sprite = await ResourceManager.Instance.GetSprite(spriteKey,"sprites/");
        this.node.getComponent(Sprite).spriteFrame = sprite;
    }

    private _onTouchStart()
    {
        this.StarTouched.DoEvent(this,this.Type);
    }
}


